/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.web.dao.support.hbn.listener;

import org.hibernate.HibernateException;
import org.hibernate.event.SaveOrUpdateEvent;
import org.hibernate.event.SaveOrUpdateEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 在自动为entity添加审计信息的Hibernate EventListener.
 * <p/>
 * 在hibernate执行saveOrUpdate()时,自动为AuditableEntity的子类添加审计信息.
 *
 * @author vacoor
 */
public class AuditListener implements SaveOrUpdateEventListener {

    private static final long serialVersionUID = -7481545873785342485L;
    @SuppressWarnings("unused")
    private static Logger logger = LoggerFactory.getLogger(AuditListener.class);

    public void onSaveOrUpdate(SaveOrUpdateEvent event) throws HibernateException {
        Object object = event.getObject();

        //如果对象是Auditable子类,添加审计信息.
//        if (object instanceof Auditable) {
//            Auditable entity = (Auditable) object;
//            @SuppressWarnings("unused")
//            String loginName = ""; //SpringSecurityUtils.getCurrentUserName();

            /*
            if (entity.getId() == null) {
				//创建新对象
				entity.setCreatedDate(new Date());
				entity.setCreateBy(loginName);
			} else {
				//修改旧对象
				entity.setLastModifyTime(new Date());
				entity.setLastModifyBy(loginName);

				logger.info("{}对象(ID:{}) 被 {} 在 {} 修改", new Object[] { event.getEntityName(), entity.getId(),
						loginName, new Date() });
			}
			*/
//        }
    }
}
